package club.terroir.client.producteur;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import club.terroir.client.Club_terroir;
import club.terroir.client.ControlerClubAdmin;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class ContactFactory {

	
	public static final List<Contact> Client_GS_TEST_ = Arrays.asList(
			new Contact("1","Nom","Prenom","Telephone","emaili@aaaaa", "addresse1","","codePostal","ville"),
			new Contact("2","Abadi","Patric","0451234555","abadi@aaaaa", "123  Avenue De Fronton","","31200","Toulouse"),
			 new Contact("3","GUIRAL","Antoine","01234555","antoine@aaaaa", "123  Avenue De Fronton","","47200","Limoges"),
		     new Contact("4","GUIRAL","Antoine","01234555","antoine2@aaaaa", "123  Avenue De Fronton","","31200","Toulouse"),
			 new Contact("5","GUIRAL","Benjamin","012345556","antoine2@aaaaa", "235  Avenue De Fronton","","31200","Fonbeauzard"),
			 new Contact("6","BARAU","Janine","654123","janine2@aaaaa", "123  Avenue De Fronton","31200","","Fonbeauzard"),
			 new Contact("7","Chaveau","Charles","62224123","charles2@aaaaa", "345 Avenue De Fronton","","31200","Fonbeauzard"),
			 new Contact("8","Durand","daniel","0221234555","daniel@aaaaa", "123  Avenue De Fronton","","46200","Cahors"),
			 new Contact("9","Eschard","Etuienne","12300","Etienne@aaaaa", "345 Avenue De Fronton","","31200","Rodez"),
			 new Contact("10","Faille","Fabrice","401234555","Fabrice@aaaaa", "123  Avenue De Fronton","","81200","Albi")
				 
			 );

	public   List<Contact> listContacts = new ArrayList<Contact>();
	static public ContactFactory  instance = new ContactFactory();;
	
	public void initListContacts() {
		Club_terroir.greetingService.getListContacts(new AsyncCallback<List<Contact>>() {
			
			@Override
			public void onSuccess(List<Contact> result) {
				listContacts=result;
				ContactsListGUI.instance.initListDataProvider();
				ControlerClubAdmin.instance.displayListeClients();
			}
			
			@Override
			public void onFailure(Throwable e) {
				e.printStackTrace();
			}
		});
	}

	
	public  Collection<Contact> getSuggestContacts(Contact contactTemp) {
		Set<Contact> setContact = new HashSet<Contact>();
		
		for(Contact c : listContacts){
			boolean bNameOk = (contactTemp.name.trim().length()==0)||(contactTemp.name.equalsIgnoreCase(c.name));
			boolean bFirstOk = (contactTemp.firstName.trim().length()==0)||(contactTemp.firstName.equalsIgnoreCase(c.firstName));
			boolean bTelephone = (contactTemp.telephone.trim().length()==0)||(contactTemp.telephone.equalsIgnoreCase(c.telephone));
			boolean bEmail = (contactTemp.email.trim().length()==0)||(contactTemp.email.equalsIgnoreCase(c.email));
			boolean bCodePostalOK = (contactTemp.codePostal.length()==0)||(contactTemp.codePostal.equalsIgnoreCase(c.codePostal));
			boolean bVilleOk = (contactTemp.ville.length()==0)||(contactTemp.ville.equalsIgnoreCase(c.ville));
			if (bNameOk && bFirstOk && bTelephone && bEmail && bCodePostalOK && bVilleOk 	){
				setContact.add(c);
			}			
		}
		return setContact;
	}
	
	public  Collection<String> getSuggestListId(Collection<Contact>  listSuggest) {
		Set<String> listId = new HashSet<String>();
		for(Contact c : listSuggest){
			listId.add(c.id.trim());			
		}
		return listId;
	}
	public  Collection<String> getSuggestListName(Collection<Contact>  listSuggest) {
		Set<String> listName = new HashSet<String>();
		for(Contact c : listSuggest){
			listName.add(c.name.trim());
			
		}
		return listName;
	}


	public  Collection<String> getSuggestListFistName(Collection<Contact>  listSuggest) {
			Set<String> set = new HashSet<String>();
			for(Contact c : listSuggest){
				set.add(c.firstName);
			}
			return set;
	}


	public  Collection<String> getSuggestListTelephone(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.telephone);
		}
		return set;
	}
	
	public  Collection<String> getSuggestListCodeEmail(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.email);
		}
		return set;
	}

	public  Collection<String> getSuggestListAddress1(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.address1);
		}
		return set;
	}
	public  Collection<String> getSuggestListAddress2(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.address2);
		}
		return set;
	}




	public  Collection<String> getSuggestListCodePostal(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.codePostal);
		}
		return set;
	}

	public  Collection<String> getSuggestListVille(Collection<Contact> listContactSuggest) {
		Set<String> set = new HashSet<String>();
		for(Contact c : listContactSuggest){
			set.add(c.ville);
		}
		return set;
	}


	

}
